<template>
	<view class="login">
		<view class="loginstyle">
			<text>用户名：</text>
			<input type="text" class="nameinput" v-model="name" />
		</view>
		<view class="loginstyle">
			<text>密码：</text>
			<input password="true" class="passinput" v-model="pass" />
		</view>
		<view class="buttonstyle">
			<button @click="regFn">注册</button>
			<button @click="loginFn">登录</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: "", //输入框输入的用户名
				pass: '', //输入框输入的密码
			}
		},
		methods: {
			// 注册函数
			regFn() {
				uni.request({
					url: "http://47.94.6.186:3001/api/user/reg",
					method: "POST",
					data: {
						loginname: this.name,
						password: this.pass
					},
					success: (res) => {
						console.log(res);
						uni.showToast({
							title: res.data.message,
							icon: 'success',
							duration: 2000,
						});
						this.name = '',
							this.pass = ''
					}
				})
			},
			// 登录函数
			loginFn() {
				uni.request({
					url: "http://47.94.6.186:3001/api/user/login",
					method: "POST",
					data: {
						loginname: this.name,
						password: this.pass
					},
					success: (res) => {
						console.log(res);
						uni.setStorageSync('token', res.data.data.token)
						uni.setStorageSync('userId', res.data.data.userid)
						uni.showToast({
							title: res.data.message,
							icon: 'success',
							duration: 2000,
						});
						this.name = '',
							this.pass = '',
							setTimeout(() => {
								uni.switchTab({
									url: "/pages/index/index"
								})
							}, 2000)
					}
				})
			}
		}
	}
</script>

<style>
	.login {
		width: 70%;
		border: 2rpx solid black;
		padding: 30rpx;
		margin: auto;
	}

	.loginstyle {
		display: flex;
		justify-content: space-between;
		margin: 15rpx;
	}

	.nameinput,
	.passinput {
		border: 2rpx solid black;
	}

	.buttonstyle {
		display: flex;
	}

	button {
		width: 40%;
		background-color: aliceblue;
	}
</style>